//
//  UIFont+ZMExtension.swift
//  OnlineClassRoom
//
//  Created by 智蚂优服 on 2020/5/12.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import Foundation
import UIKit

extension UIColor : ZMCompatible{}

extension ZM where Base == UIColor {
    /// 通过指定的不透明度和 RGB 分量值, 返回一个颜色对象
    ///
    /// - Parameters:
    ///   - red: 红色分量的值 (0 ~ 255)
    ///   - green: 绿色分量的值 (0 ~ 255)
    ///   - blue: 蓝色分量的值 (0 ~ 255)
    ///   - alpha: 不透明度的值 (0 ~ 1)
    /// - Returns: 颜色对象
    public static func RGBA(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> UIColor {
        return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
    }
    
    /// 通过指定的不透明度 和 一个用16进制数字表示 RGB 分量值的字符串, 返回一个颜色对象
    ///
    /// - Parameters:
    ///   - hexString: 以"#"或"0x"开头, 后面跟随6位(或3位)16进制数字 表示RGB分量值的字符串
    ///   - alpha: 不透明度 (0 ~ 1)
    /// - Returns: 颜色对象
    public static func color(hexString: String, _ alpha: CGFloat = 1.0) -> UIColor {
        var red: CGFloat = 0.0, green: CGFloat = 0.0, blue:  CGFloat = 0.0
        var cString = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines).uppercased()
        
        guard cString.hasPrefix("0X") || cString.hasPrefix("#") || cString.zm.length == 6 else {
            return UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
        }
        
        if cString.hasPrefix("0X") {
            cString = cString.zm.substring(from: 2)
        } else if cString.hasPrefix("#") {
            cString = cString.zm.substring(from: 1)
        }
        let scanner = Scanner(string: cString)
        var hexValue: CUnsignedLongLong = 0
        guard scanner.scanHexInt64(&hexValue) else {
            return UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
        }
        
        switch (cString.count) {
        case 3:
            red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
            green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
            blue  = CGFloat(hexValue & 0x00F)              / 15.0
            
        case 6:
            red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
            green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
            blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
            
        default:
            red = 0.0; green = 0.0; blue = 1.0
        }
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
    
    public static func colorBase() -> UIColor {
        return UIColor.zm.color(hexString: Style.mainColorStr)
    }
    
    /// 颜色值333333
    /// - Returns: 333333
    public static func color333333() -> UIColor {
        return UIColor.zm.color(hexString: "#333333")
    }
    
    /// 颜色值F7F7F7
    /// - Returns: F7F7F7
    public static func colorF7F7F7() -> UIColor {
        return UIColor.zm.color(hexString: "#F7F7F7")
    }
    
    /// 颜色值999999
    /// - Returns: 999999
    public static func color999999() -> UIColor {
        return UIColor.zm.color(hexString: "#999999")
    }
    
    /// 颜色值CCCCCC
    /// - Returns: CCCCCC
    public static func colorCCCCCC() -> UIColor {
        return UIColor.zm.color(hexString: "#CCCCCC")
    }
    
    /// 颜色值666666
    /// - Returns: 666666
    public static func color666666() -> UIColor {
        return UIColor.zm.color(hexString: "#666666")
    }
    
    /// 颜色值F2F2F2
    /// - Returns: F2F2F2
    public static func colorF2F2F2() -> UIColor {
        return UIColor.zm.color(hexString: "#F2F2F2")
    }
    
    /// 颜色值F5F5F5
    /// - Returns: F5F5F5
    public static func colorF5F5F5() -> UIColor {
        return UIColor.zm.color(hexString: "#F5F5F5")
    }
    
    public static func officialApplePlaceholderGray() -> UIColor {
        return UIColor(red: 0, green: 0, blue: 0.0980392, alpha: 0.22)
    }
    
    public static func randomColor() -> UIColor {
        return UIColor.init(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    }
}
